<?php
/**
 * (c) страйкер#php@irc.rusnet.ru
 * с изменениями
 * */

//define ('_USERAGENT','Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729)');
//define ('_USERAGENT','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MRA 5.1 (build 02190); Avant Browser; .NET CLR 1.1.4322)');

class CURL
{
    protected $ch = null;
    protected $closed = false;

    protected $url = null;
    protected $options = array();

    protected $stdOptions = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729) : YeAP',
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false
        );

    public function __construct($url = null)
    {
        $this->ch = curl_init($url);
        $this->url = $url;

        $this->setOpts($this->stdOptions);
    }

    public function setOpt($name, $value)
    {
        $this->options[$name] = $value;
        curl_setopt($this->ch, $name, $value);
    }

    public function setOpts(Array $options)
    {
        foreach ($options as $name => $value) {
            $this->setOpt($name, $value);
        }
    }

    public function exec()
    {
        $verb = curl_exec($this->ch);
        if (curl_error($this->ch) !== '') {
            //throw new Exception('curl error: ' . curl_error($this->ch), curl_errno($this->ch));
            throw new ErrorException('curl error: ' . curl_error($this->ch), curl_errno($this->ch));
        }
        return $verb;
    }

    public function post($post)
    {
        $this->setOpt(CURLOPT_POST, true);
        $this->setOpt(CURLOPT_POSTFIELDS, $post);
    }

    public function getInfo($info = 0)
    {
        return curl_getinfo($this->ch, $info);
    }

    public function &getHandler()
    {
        return $this->ch;
    }

    public function close()
    {
        $this->closed = true;
        curl_close($this->ch);
    }

    public function __destruct()
    {
        if (!$this->closed) {
            $this->close();
        }
    }
}
?>